package com.example.mediamoudle.main.activity

import android.graphics.Rect
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.android.baselibrary.arouter.ArouterContains
import com.android.baselibrary.base.BaseActivity
import com.example.mediamoudle.R
import com.example.mediamoudle.main.adapter.MediaAdapter
import com.example.mediamoudle.main.bean.MediaBean
import com.example.mediamoudle.main.bean.MediaDataUtils
import com.example.mediamoudle.databinding.ActivityMediaMainBinding
import com.example.mediamoudle.utlis.MediaSpaceDecoration
import com.example.mediamoudle.utlis.dp2px

@Route(path = ArouterContains.MEDIA_MOUDLE)
class MediaMainActivity : BaseActivity<ActivityMediaMainBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_media_main

    override fun initView() {
        val mediaAdapter = MediaAdapter()
        mBinding.mediaRecycle.addItemDecoration(MediaSpaceDecoration(topSpace = 15))
         val manager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        mBinding.mediaRecycle.itemAnimator = null
        mBinding.mediaRecycle.layoutManager = manager
        manager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_NONE //防止错位
        val divider = dp2px(8f).toInt()
         val itemDecoration :RecyclerView.ItemDecoration = object :RecyclerView.ItemDecoration(){
             override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                 super.getItemOffsets(outRect, view, parent, state)
                 val layoutParams :StaggeredGridLayoutManager.LayoutParams = view.layoutParams as StaggeredGridLayoutManager.LayoutParams
                 val spanIndex = layoutParams.spanIndex
                 val position = parent.getChildAdapterPosition(view)
                 outRect.bottom = divider
                 if (position == 0 || position == 1){
                     outRect.top = divider *2
                 }else{
                     outRect.top = 0
                 }
                 if (spanIndex % 2 == 0) { //左边
                     outRect.left = divider
                     outRect.right = divider / 2
                 } else {// 右边
                     outRect.left = divider / 2
                     outRect.right = divider
                 }

             }
         }
        mBinding.mediaRecycle.addItemDecoration(itemDecoration)
         mBinding.mediaRecycle.adapter = mediaAdapter
         mediaAdapter.setData(MediaDataUtils.getMediaData)
        mediaAdapter.setonClickItemListener (this::click)

    }

    override fun initListener() {

    }
    private fun click(data: MediaBean){
        Log.i("TAG", "click: ")
    ARouter.getInstance().build(data.path).navigation()
    }
}